<template>
	<view v-if="isShowTemplate">
		<view class="main_box">
			<index v-if="currentIndex === 0&&tabListNum==1" :tabListNum="tabListNum"></index>
			<index2 v-if="currentIndex === 0&&tabListNum==2" :tabListNum="tabListNum"></index2>
			<index3 v-if="currentIndex === 0&&tabListNum==3" :tabListNum="tabListNum"></index3>
			<index4 v-if="currentIndex === 0&&tabListNum==4" :tabListNum="tabListNum"></index4>
			<shop v-if="currentIndex === 1"></shop>

			<packageCard v-if="currentIndex === 3"></packageCard>
			<distribution v-if="currentIndex === 4"></distribution>
		</view>
		<view class="foot_box">
			<TabBar :tabIndex="currentIndex" :showMiddleButtonBar="showMiddleButtonBar" @changeItem="changeItem"
				:tabListNum="tabListNum"></TabBar>
		</view>

	</view>
</template>

<script>
	import TabBar from '../../components/jinjie-tabbar/jinjie-tabbar.vue'
	// 套餐1
	import index from '@/pages/index/index.vue'
	// 套餐2
	import index2 from '@/pages/index/index2.vue'
	// 套餐3
	import index3 from '@/pages/index/index3.vue'
	// 套餐4
	import index4 from '@/pages/index/index4.vue'
	// 商城
	import shop from '@/pages/shop/shop.vue'
	// 套餐卡
	import packageCard from '@/pages/packageCard/packageCard.vue'
	// 分销
	import distribution from '@/pages/distribution/distribution.vue'

	export default {
		components: {

			index,
			index2,
			index3,
			index4,
			shop,
			packageCard,
			distribution,
			TabBar
		},
		data() {
			return {

				currentIndex: 0,
				//  1 模版1 2 模版2 3 模版3 4 模版4
				tabListNum: 3,
				showMiddleButtonBar: true,
				isShowTemplate: false,
				token: uni.getStorageSync('login').token,
				userId: '',
				hrefs: '',
				signParams:null,
				getConfig:null


			}
		},
		onShow(){
			this.init()
		},
		onLoad(options) {

			
			// let getUrl = window.location.href.split('#')[0];
			// console.log('options', options);
            // https://ship.admin168.net/H5/index.html?id=2&currentIndex=1#/
			let getUrl = 'https://ship.weijuyunke.net/H5/index.html?id=1&currentIndex=1#/'.split('#')[0];
			// let getUrl = 'https://ship.admin168.net/H5/index.html?id=5#/'.split('#')[0];
	
			if (getUrl.indexOf('id') != -1) {
				const Id = getUrl.split('?id=')[1];
				this.userId=Id.split('&')[0]
				const index=Id.split('&currentIndex=')[1]
				console.log('this.userId', this.userId)
				console.log('index',index)
			
				// return
				uni.setStorageSync('userId', this.userId); //缓存登录用户的id
				this.init()
				if(index==2){
					this.$sun.navGo('/pages/qRCodePayment/qRCodePayment')
				}else if(!(index==undefined)){
					this.currentIndex = Number(index)
				}else{
					this.currentIndex = Number(0)
				}
				
			} else {
				if (options.userId) {
					this.userId = options.userId;
					uni.setStorageSync('userId', this.userId); //缓存登录用户的id
				}else{
					this.init()
				}

			}

			if (Object.keys(options).length) {
				console.log(options)
				if (options.currentIndex) {
					this.currentIndex = Number(options.currentIndex)
					console.log(this.currentIndex)
					this.templateFn()
				}
			}
			
		},
		methods: {
			
			
			
			async init() {
				

				const ress = await this.$http.post({
					url: this.$api.userinfo,
					data: {

						id: uni.getStorageSync('userId'),

					},


				})
				console.log(ress)
				if (ress.code == 0) {
					uni.setStorageSync('login', ress.data)


					// 跳转到登录页面

					this.$sun.toast(ress.msg, 'none')
					// 获取当前选取的模版

					this.templateFn()



				} else {

					this.$sun.toast(ress.msg, 'none')
				}

			},
			// 获取模版数据
			async templateFn() {
				const res = await this.$http.post({
					url: this.$api.template,
					data: {

					},
					headers: {
						'Authorization': `Bearer ${uni.getStorageSync('login').token}`
					}

				})

				if (res.code == 0) {

					console.log(res)


					// 根据id 来判断当前是什么模版
					this.isShowTemplate = true
					if (res.data.status == 1) {
						this.tabListNum = res.data.type_id
						// this.tabListNum =1
					} else {
						this.tabListNum = 1
					}
					// 获取当前选取的模版





				} else {

					this.$sun.toast(res.msg, 'none')
				}
			},
			// 如果
			changeItem(item) {
				console.log(item)
				if (item == 'selsectCode') {
					console.log('弹出二维码')
					this.$sun.navGo('/pages/qRCodePayment/qRCodePayment')

				} else {
					this.currentIndex = item
				}


			},
			close() {
				debugger
				this.active = false
			},
			open() {},


		},

		watch: {
			tabListNum: {
				immediate: true,
				handler(newValue, oldValue) {
					// 在值发生变化时执行操作
					console.log('数值发生变化：', newValue);
					// if (newValue == 3 || newValue == 4) {
					// 	this.showMiddleButtonBar = false
					// }
				},
			},
		},
	}
</script>

<style lang="scss" scoped>
	.main_box {
		height: calc(100vh - 100rpx);
		overflow: scroll;
	}

	.foot_box {
		height: 110rpx;
	}



	/deep/ .u-popup__content {
		border-radius: 30rpx 30rpx 0 0;
	}
</style>